<?php

declare(strict_types=1);

namespace App\Admin\Service\System;

use App\Admin\Mapper\System\SystemNoticeMapper;
use App\Admin\Model\System\SystemQueueMessage;
use App\Admin\Vo\System\QueueMessageVo;
use Newbie\Abstracts\AbstractService;
use Newbie\exception\NormalStatusException;

/**
 * 通知管理服务类
 */
class SystemNoticeService extends AbstractService {
    /**
     * @var SystemNoticeMapper
     */
    public $mapper;

    public function __construct (SystemNoticeMapper $mapper) {
        $this->mapper = $mapper;
    }

    /**
     * 保存公告
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Throwable
     * @throws \Psr\Container\NotFoundExceptionInterface
     */

    public function save (array $data): int {
        $message = new QueueMessageVo();
        $message->setTitle($data['title']);
        $message->setContentType(
            $data['type'] === '1'
                ? SystemQueueMessage::TYPE_NOTICE
                : SystemQueueMessage::TYPE_ANNOUNCE
        );
        $message->setContent($data['content']);
        $message->setSendBy(user()->getId());
        $data['message_id'] = push_queue_message($message, $data['users']);

        if ($data['message_id'] !== -1) {
            return parent::save($data);
        }

        throw new NormalStatusException;
    }

}
